package com.sxtyxt.online;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

/**
 * Web浏览
 *
 * @author 王磊杰
 */
public class ActWeb extends ActBase {
    private WebView webView;
    private String url;
    private boolean isError = false;
    private ProgressBar pb;

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case 0:
                    int percent = (int) msg.arg1;
                    pb.setVisibility(View.VISIBLE);
                    pb.setProgress(percent);
                    break;
                case 1:
                    pb.setVisibility(View.INVISIBLE);
                    break;
                case 2:
                    pb.setVisibility(View.INVISIBLE);
                    break;
                default:
                    break;
            }
            super.handleMessage(msg);
        }

    };

    @Override
    protected void initUI() {

        super.initUI();
        url = "http://b2b2c.leimingtech.com/leimingtech-front/";
        url=getIntent().getStringExtra("url");

        String title=getIntent().getStringExtra("title");
        if(!TextUtils.isEmpty(title)){
//            TextView txt_title=(TextView)findViewById(R.id.txt_title);
//            txt_title.setText(title);
            mTitleBar.setVisibility(View.VISIBLE);
            mTitleBar.mSetTitle(title);
        }else{
            mTitleBar.setVisibility(View.GONE);
        }


        webView = (WebView) findViewById(R.id.webView);
        pb=(ProgressBar)findViewById(R.id.pb);

        WebSettings webSettings =   webView .getSettings();
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);
        webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setJavaScriptEnabled(true);
//		webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//				loading(view, url);
//				view.loadUrl(url);
//				return true;

                if(url.equals("https://www.baidu.com/")){
                    finish();
                    return true;
                }


                if (url.startsWith("mailto")) {
                    // TODO: extract the email... that's your work, LOL
                    sendEmail(url.replace("mailto:", ""));
                    return true;
                }
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {
                isError = true;
                view.stopLoading();
                Message msg = handler.obtainMessage();
                msg.what = 2;// 加载失败
                handler.sendMessage(msg);
            }

        });

        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {

                if (newProgress >= 100 && !isError) {
                    handler.sendEmptyMessage(1);
                } else
                if (newProgress > 10 && newProgress < 100 && !isError) {

                    Message msg = Message.obtain();
                    msg.what = 0;
                    msg.arg1 = newProgress;
                    handler.sendMessage(msg);
                } else
                if (newProgress <= 10 && !isError) {

                    Message msg = Message.obtain();
                    msg.what = 0;
                    msg.arg1 = 0;
                    handler.sendMessage(msg);
                }

                super.onProgressChanged(view, newProgress);
            }
        });
        webView.loadUrl(url);
    }



    @Override
    protected int getLayoutId() {
        return R.layout.web;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    public void sendEmail(String email){
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("plain/text");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{email});

        String mySubject = "this is just if you want";
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, mySubject);
        String myBodyText = "this is just if you want";
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, myBodyText);
        startActivity(Intent.createChooser(emailIntent, "Send mail..."));
    }

}
